﻿using CatUtils.Utils.Currency;
using CatUtils.Utils.Currency.IoUtils;
using CatUtils.Utils.Helper;
using SinaImgUpload.SainTool.LocalTool;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SinaImgUpload.UserView.ServerTool
{
    internal class UploadBatch
    {
        public static readonly string ServerUploadPath = StaticValue.BinPath + "AsyncFloerPath.txt";

        /// <summary>
        /// 添加需要保存的文件夹
        /// </summary>
        public void AddFloer()
        {
            string FloderPath = FloderHelper.SelectFloder();
            if (FloderPath == "")
                return;
            List<string> FloderList = FileHelper.ReadAllLine(ServerUploadPath);
            FloderList.Add(FloderPath);
            FloderList = ListHelper.ListRepeat(FloderList);
            FileHelper.WriteList(ServerUploadPath, FloderList);
        }

        public bool UploadFile(string[] FileList)
        {
            foreach (var item in FileList)
            {
                string Content = FileHelper.ReadAllText(item);
                if (Content == "")
                {
                    Console.WriteLine("文件未能读取到内容，跳过上传。");

                    return false;
                }
                else
                {
                    string UploadFileName = new UploadListFile().UpListFile(item, false);
                    Console.WriteLine("上传完毕" + UploadFileName);
                    UploadToBaidu(UploadFileName);
                }
            }

            return true;
        }

        /// <summary>
        /// 上传文件到百度
        /// </summary>
        /// <param name="UploadFileName"></param>
        public void UploadToBaidu(string UploadFileName)
        {
            if (UploadFileName == "")
                return;
            string FloderName = "/list/";
            string CreateFloderCommend = $"bypy mkdir {FloderName}";
            //    string UploadCommend = $"bypy  upload {UploadFileName}  {FloderName} \r\n del  {FloderNameHelper.GetFloderPath(UploadFileName).Replace("上传到图床\\", "")}  /Q \r\n del {FloderNameHelper.GetFloderPath(UploadFileName)} /Q";
            string UploadCommend = $"bypy  upload {UploadFileName}  {FloderName} /Q ";

            RunCommend("CreateDir", CreateFloderCommend);
            RunCommend("UploderFile", UploadCommend);
        }

        /// <summary>
        /// 执行代码，通过bat
        /// </summary>
        private void RunCommend(string BatName, string Commend)
        {
            Console.WriteLine("BAT创建" + $"{StaticValue.TempPath}{BatName}.bat");
            FileHelper.WriteText($"{StaticValue.TempPath}{BatName}.bat", Commend, Encoding.Default);
            FloderHelper.OpenFloder($"{StaticValue.TempPath}{BatName}.bat");
        }
    }
}